﻿Imports Autodesk.AutoCAD.ApplicationServices.Application



Public Class FileBrowser

    Dim ACA As New AutocadAddInAutomation
    Dim ARASCONN As ConnecteurAras
    Dim ARASURL As String
    Dim ARASDB As String
    Dim ARASUN As String
    Dim ARASUP As String


    Private Sub FileToOpen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Reload_Form()

    End Sub

    Public Sub Reload_Form()
        Dim folder As String = My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData
        Dim GetParam As New ComINI
        ARASURL = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "ARASURL")
        ARASDB = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "ARASDB")
        ARASUN = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "ARASUN")
        ARASUP = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "ARASUP")

        ARASCONN = New ConnecteurAras(ARASURL, ARASDB, ARASUN, ARASUP)
        Dim Grid_1_DataSources As New DataSet
        Dim AutocadDocColName(50) As String
        Dim DTOColName(50) As String



        ' 1a-List all the item with itemtyp "Prodeos_DTO" from the Actual User
        DTOColName = ARASCONN.GetDocToOpenProperties()
        Dim ItemFlaggedList As New MSXML2.DOMDocument
        ItemFlaggedList = ARASCONN.GetItemListFlagged(ARASUN)

        ' 1b-List all the Autocad items available for the actual user
        AutocadDocColName = ARASCONN.GetAutocadDocProperties()
        Dim AutocadItemList As New MSXML2.DOMDocument
        AutocadItemList = ARASCONN.GetAutocadItemList()
        Dim Grid_2_DataSources As New DataSet


        ' 2-for each item check if it is available in the Cache and if the version is older


        ' 3-Render the datagrids
        Dim tabletoshow1 As New DataTable
        Dim tabletoshow2 As New DataTable
        Grid_1_DataSources = ACA.FormatForDataGrid(ItemFlaggedList, DTOColName)
        Grid_2_DataSources = ACA.FormatForDataGrid(AutocadItemList, AutocadDocColName)
        tabletoshow1 = Grid_1_DataSources.Tables(0)
        tabletoshow2 = Grid_2_DataSources.Tables(0)
        DataGridView1.DataSource = tabletoshow1
        DataGridView2.DataSource = tabletoshow2
    End Sub


    Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
        Dim DocFilePath As String = ""
        Dim DocFileNameToOpen As String = ""
        Dim ACAD_DocHandle As Autodesk.AutoCAD.ApplicationServices.Document
        Dim ItemRowNbr As Integer
        Dim CachedFileState, ItemID, FileStatus, filename, DTOid, DocClasse As String
        Dim CheckWarningResult As MsgBoxResult

        ' get informations of selected item
        ItemRowNbr = DataGridView1.SelectedCells(0).RowIndex
        DTOid = DataGridView1.Rows(ItemRowNbr).Cells("keyed_name").Value
        ItemID = DataGridView1.Rows(ItemRowNbr).Cells("docid").Value
        CachedFileState = DataGridView1.Rows(ItemRowNbr).Cells("Cached File").Value
        FileStatus = DataGridView1.Rows(ItemRowNbr).Cells("statut_dto").Value
        filename = DataGridView1.Rows(ItemRowNbr).Cells("documentname").Value
        DocClasse = DataGridView1.Rows(ItemRowNbr).Cells("classe").Value
        DocFilePath = ARASCONN.getLocalWorkFolder(ARASUN)
        CheckWarningResult = MsgBoxResult.Ok


        If FileStatus <> "Create" Then

            If CachedFileState = "Unknown" Then
                CheckWarningResult = MsgBox("Continue Opening without checking if a cached version of the file exists?", MsgBoxStyle.OkCancel)
            End If

            If CheckWarningResult <> MsgBoxResult.Cancel Then

                If CachedFileState <> "Up to Date" Then
                    'CheckOut Aras File
                    Dim doctype(1) As String
                    doctype(0) = "AUTOCAD"
                    doctype(1) = "dwg"
                    ARASCONN.CheckOutDocFile(ItemID, DocFilePath, doctype, ARASURL, ARASDB)
                End If

                ' Open the cache file
                ACAD_DocHandle = DocumentManager.Open(DocFilePath & "/" & filename, False)
                ' Add Document Informations
                ACAD_DocHandle.UserData.Add("Document ID", ItemID)
                ACAD_DocHandle.UserData.Add("Document name", filename)
                ACAD_DocHandle.UserData.Add("UserLocalDir", DocFilePath)
                'supprimer le DTO
                ARASCONN.DeleteDTOitem(DTOid)
                Dim Ribbon As New AutoCAD_VB_plug_in1.AdskCommandHandler
                Ribbon.UpdateRibbonButtons()
                Me.Close()
            End If
        Else
            'Start a New Doc
            Dim UseTemplateQuestion As MsgBoxResult
            Dim boolTemplate As Boolean = False
            Dim templatefilename As String = ""


            'if class is not null check if a template exists
            If DocClasse <> "" Then
                UseTemplateQuestion = MsgBox("Do you want to use the available template?", MsgBoxStyle.YesNo)
                If UseTemplateQuestion = MsgBoxResult.Yes Then
                    'download the template file
                    templatefilename = ARASCONN.DownloadTemplate(DocClasse, DocFilePath)
                    If templatefilename <> "" Then
                        boolTemplate = True
                    End If
                End If
            End If

            If boolTemplate = False Then
                ACAD_DocHandle = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Add("acad.dwt")
            Else
                ACAD_DocHandle = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Add(DocFilePath & "/" & templatefilename)
            End If

            ' Add Document Informations
            ACAD_DocHandle.UserData.Add("Document ID", ItemID)
            ACAD_DocHandle.UserData.Add("Document name", filename & ".dwg")
            ACAD_DocHandle.UserData.Add("UserLocalDir", DocFilePath)
            'supprimer le DTO
            ARASCONN.DeleteDTOitem(DTOid)
            Dim Ribbon As New AutoCAD_VB_plug_in1.AdskCommandHandler
            Ribbon.UpdateRibbonButtons()
            Me.Close()
        End If



    End Sub

    'Get Connection settings from the Local ini file
    Private Sub GetConnectionSettings(ByVal ARASURL As String, _
                                      ByVal ARASDB As String, _
                                      ByVal ARASUN As String, _
                                      ByVal ARASUP As String)

        Dim GetParam As New ComINI
        Dim folder As String = My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData

        ARASURL = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "ARASURL")
        ARASDB = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "ARASDB")
        ARASUN = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "ARASUN")
        ARASUP = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "ARASUP")

    End Sub

    Private Sub ContextMenuStrip1_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening

    End Sub

    Private Sub DeleteFromOpeningListToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteFromOpeningListToolStripMenuItem.Click
        Dim NbrItemSelected, i, RowNumber As Integer
        Dim IdToDelete As String
        ARASCONN = New ConnecteurAras(ARASURL, ARASDB, ARASUN, ARASUP)
        'delete the selected item 

        NbrItemSelected = DataGridView1.SelectedRows.Count

        For i = 0 To NbrItemSelected - 1
            'get row number
            RowNumber = DataGridView1.SelectedRows.Item(i).Index
            IdToDelete = DataGridView1.Rows(RowNumber).Cells("keyed_name").Value
            ARASCONN.DeleteDTOitem(IdToDelete)
        Next

        'refresh the list
        Reload_Form()

    End Sub

    Private Sub CheckCachedFileStatusToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckCachedFileStatusToolStripMenuItem.Click
        Dim UserLocalDir, IdToCheck As String
        Dim NbrItemSelected, i, RowNumber As Integer
        Dim Aras_FileInfo(2), Local_FileInfo(2) As String
        'get Local Folder
        ARASCONN = New ConnecteurAras(ARASURL, ARASDB, ARASUN, ARASUP)
        UserLocalDir = ARASCONN.getLocalWorkFolder(ARASUN)

        'Get filename
        NbrItemSelected = DataGridView1.SelectedRows.Count

        For i = 0 To NbrItemSelected - 1
            'get row number
            RowNumber = DataGridView1.SelectedRows.Item(i).Index
            IdToCheck = DataGridView1.Rows(RowNumber).Cells("keyed_name").Value
            'check if doc as file
            If DataGridView1.Rows(RowNumber).Cells("statut_dto").Value <> "Create" Then
                Aras_FileInfo = ARASCONN.GetFileName(IdToCheck, RowNumber)

                'get local file checksum
                Local_FileInfo = ACA.GetLocalFileinfo(Aras_FileInfo(0), UserLocalDir)

                'compare both checksum

                If Local_FileInfo(1) <> "" Then

                    If ((Local_FileInfo(1) = Aras_FileInfo(1)) And (Local_FileInfo(2) = Aras_FileInfo(2))) Then
                        'if equal ==> green line
                        DataGridView1.Rows(RowNumber).Cells("Cached File").Style.BackColor = Drawing.Color.Green
                        DataGridView1.Rows(RowNumber).Cells("Cached File").Value = "Up to Date"
                    Else
                        'if different ==> Orange line
                        DataGridView1.Rows(RowNumber).Cells("Cached File").Style.BackColor = Drawing.Color.Orange
                        DataGridView1.Rows(RowNumber).Cells("Cached File").Value = "Obsolete"
                    End If

                Else
                    'file is not in local
                    DataGridView1.Rows(RowNumber).Cells("Cached File").Style.BackColor = Drawing.Color.Brown
                    DataGridView1.Rows(RowNumber).Cells("Cached File").Value = "No File"
                End If
            Else
                DataGridView1.Rows(RowNumber).Cells("Cached File").Style.BackColor = Drawing.Color.AliceBlue
                DataGridView1.Rows(RowNumber).Cells("Cached File").Value = "No File"
            End If

        Next
        DataGridView1.ClearSelection()


    End Sub

    Private Sub RefreshListToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RefreshListToolStripMenuItem.Click
        Reload_Form()

    End Sub

    Private Sub RefreshListToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RefreshListToolStripMenuItem1.Click
        Reload_Form()

    End Sub

    Private Sub FlagForWorkToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FlagForWorkToolStripMenuItem.Click
        'Create a DTO document
        Dim Item_Nbr As String
        Dim RowNbr As Integer

        RowNbr = DataGridView2.SelectedCells.Item(0).RowIndex
        Item_Nbr = DataGridView2.Rows(RowNbr).Cells("item_number").Value
        ARASCONN.CreateDTOfromItemNbr(Item_Nbr)

        Reload_Form()

    End Sub



    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub

    Private Sub DataGridView2_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView2.CellContentClick

    End Sub
End Class